package com.example.soa_exp2.controller;

import com.example.soa_exp2.common.EquipmentModel;
import com.example.soa_exp2.common.MessageModel;
import com.example.soa_exp2.domain.Equipment;
import com.example.soa_exp2.persistence.EquipmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.CollectionModel;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

@RestController
public class EquipmentController {

    @Autowired
    private EquipmentMapper equipmentMapper;

    @GetMapping("/equipment")
    public HttpEntity<CollectionModel<EquipmentModel>> getAllEquipment() {
        List<Equipment> equipmentList = equipmentMapper.selectList(null);
        List<EquipmentModel> equipmentModels = new ArrayList<>();
        for(Equipment equipment:equipmentList){
            EquipmentModel model = new EquipmentModel(equipment);
            model.add(linkTo(methodOn(EquipmentController.class).getEquipmentById(equipment.getEquipmentid())).withSelfRel());
            equipmentModels.add(model);
        }
        CollectionModel<EquipmentModel> models = CollectionModel.of(equipmentModels);
        models.add(linkTo(methodOn(EquipmentController.class).getAllEquipment()).withSelfRel());
        return new ResponseEntity<>(models, HttpStatus.OK);
    }

    @GetMapping("/equipment/{equipmentId}")
    public HttpEntity<EquipmentModel> getEquipmentById(@PathVariable String equipmentId){
        Equipment equipment = equipmentMapper.selectById(equipmentId);
        if(equipment == null){
            EquipmentModel model = new EquipmentModel(null, "没有查询到设备");
            model.add(linkTo(methodOn(EquipmentController.class).getAllEquipment()).withRel("allEquipment"));
            return new ResponseEntity<>(model, HttpStatus.OK);
        }else {
            EquipmentModel model = new EquipmentModel(equipment);
            model.add(linkTo(methodOn(EquipmentController.class).getEquipmentById(equipmentId)).withSelfRel());
            return new ResponseEntity<>(model, HttpStatus.OK);
        }
    }

    @PostMapping("/equipment")
    public HttpEntity<EquipmentModel> addEquipment(@RequestBody Equipment addEquipment){
        equipmentMapper.insert(addEquipment);
        EquipmentModel model = new EquipmentModel(addEquipment);
        model.add(linkTo(methodOn(EquipmentController.class).getEquipmentById(addEquipment.getEquipmentid())).withSelfRel());
        return new ResponseEntity<>(model, HttpStatus.OK);
    }

    @PutMapping("/equipment/{equipmentId}")
    public HttpEntity<EquipmentModel> updateEquipmentById(@PathVariable String equipmentId, @RequestBody Equipment newEquipment){
        newEquipment.setEquipmentid(equipmentId);
        equipmentMapper.updateById(newEquipment);
        EquipmentModel model = new EquipmentModel(newEquipment);
        model.add(linkTo(methodOn(EquipmentController.class).updateEquipmentById(equipmentId, newEquipment)).withSelfRel());
        return new ResponseEntity<>(model, HttpStatus.OK);
    }

    @DeleteMapping("/equipment/{equipmentId}")
    public HttpEntity<MessageModel> deleteEquipmentById(@PathVariable String equipmentId){
        equipmentMapper.deleteById(equipmentId);
        MessageModel model = new MessageModel("删除成功！");
        model.add(linkTo(methodOn(EquipmentController.class).getAllEquipment()).withRel("allUser"));
        return new ResponseEntity<>(model, HttpStatus.OK);
    }
}
